package com.summer.mall.controller;

import com.summer.mall.common.Const;
import com.summer.mall.common.ResponseCode;
import com.summer.mall.common.ResponseService;
import com.summer.mall.dto.user.*;
import com.summer.mall.entity.User;
import com.summer.mall.expection.MallException;
import com.summer.mall.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserService userService;

    @Autowired
    private HttpSession session;

    @RequestMapping(value = "/test",method = RequestMethod.POST)
    @ResponseBody
    public ResponseService<String> test(){
       ResponseService<String> test = userService.testUser();

       return test;
    }

    @RequestMapping(value = "/logout",method = RequestMethod.POST)
    @ResponseBody
    public ResponseService<User> logout(){
        session.removeAttribute(Const.CURRENT_USER);
        return ResponseService.createBySuccess();
    }


    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public ResponseService<User> login(@RequestBody UserDTO userDTO){

        ResponseService<User> response = userService.login(userDTO.getUsername(),userDTO.getPassword());
        if (response == null){
            throw new MallException(ResponseCode.RESPONSE_NOT_EXIST);
        }

        if(response.isSuccess()){

            session.setAttribute(Const.CURRENT_USER ,response.getData());
        }


        return response;
    }


    @RequestMapping(value = "/regist",method = RequestMethod.POST)
    @ResponseBody
    public ResponseService<String> regist(@RequestBody User user){

        return userService.regist(user);
    }

    //校验用户名和邮箱是否存在 str 对应(用户名/邮箱)字符串的内容 type 对应 字段usernam / email
    @RequestMapping( value = "/checkValid", method = RequestMethod.POST)
    @ResponseBody
    public ResponseService<String> checkValid(@RequestBody UserVerificationDTO userVerificationDTO){

        return userService.checkValid(userVerificationDTO.getStr(), userVerificationDTO.getType());

    }

    //获取用户登录信息
    @RequestMapping(value ="/get_user_info",method = RequestMethod.GET)
    @ResponseBody
    public ResponseService<User> getUserInfo(){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user != null){
            user.setPassword("");
            return ResponseService.createBySuccess(user);
        }
        return ResponseService.createByErrorMessage("用户未登录,无法获取当前用户信息");
    }

    /**
     * 忘记密码获取问题
     * @param username
     * @return
     */
    @RequestMapping(value ="/forget_get_question",method = RequestMethod.POST)
    @ResponseBody
    public ResponseService<String> getQuestion(@RequestBody UserQuestionByUsernameDTO userQuestionByUsernameDTO){
        return userService.getQuestionByUserName(userQuestionByUsernameDTO.getUsername());

    }



    /**
     * 忘记密码验证回答
     * @param username
     * @param question
     * @param answer
     * @return
     */
    @RequestMapping(value ="/forget_check_answer",method = RequestMethod.POST)
    @ResponseBody
    public ResponseService<String> forgetCheckAnswer(@RequestBody UserAnswerDTO userAnswerDTO){
        return userService.checkAnswer(userAnswerDTO.getUsername(),userAnswerDTO.getQuestion(),userAnswerDTO.getAnswer());
    }

    /**
     *验证通过后重置密码，带着上次返回的token
     * @param username
     * @param passwordNew
     * @param forgetToken
     * @return
     */
    @RequestMapping(value ="/forget_reset_password",method = RequestMethod.POST)
    @ResponseBody
    public ResponseService<String> forgetResetPassword(@RequestBody UserForgetResetPasswordDTO userForgetResetPasswordDTO){
        return userService.forgetResetPassword(userForgetResetPasswordDTO.getUsername(),userForgetResetPasswordDTO.getPasswordNew(),userForgetResetPasswordDTO.getForgetToken());
    }
    //登录状态的重置密码
    @RequestMapping(value ="/reset_password",method = RequestMethod.POST)
    @ResponseBody
    public ResponseService<String> resetPassword(@RequestBody UserResetPaswordDTO userResetPaswordDTO){

        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ResponseService.createBySuccessMessage("用户未登录,请登录!!!");
        }
        return userService.resetPassword(user,userResetPaswordDTO.getPasswordNew(),
                                        userResetPaswordDTO.getPasswordOld());

    }


    //更新用户信息 用户ID和username 不能更改
    @RequestMapping(value ="/update_information",method = RequestMethod.POST)
    @ResponseBody
    public ResponseService<User> updateInformation(@RequestParam String newUserName){
        User currentUser = (User)session.getAttribute(Const.CURRENT_USER);

        if(currentUser == null){
            return ResponseService.createByErrorMessage("用户未登录,请登录!!!");
        }
        currentUser.setUsername(newUserName);
        System.out.println(newUserName);
        ResponseService<User> response= userService.updateInformation(currentUser);
        if(response.isSuccess()){
            session.setAttribute(Const.CURRENT_USER,response.getData());
        }
        return response;
    }

    //获取用户的个人信息
    @RequestMapping(value ="/get_information",method = RequestMethod.GET)
    @ResponseBody
    public ResponseService<User> getInformation(){
        User currentUser = (User)session.getAttribute(Const.CURRENT_USER);
        if(currentUser == null){
            return ResponseService.createByCodeErrorMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,无法获取当前用户信息,status=10,强制登录");
        }
        ResponseService<User> response = userService.getInformation(currentUser.getId());
        return response;
    }

}
